﻿using ASmile.Com;
using ASmile.Com.GridView;
using ASmile.DBModel;
using ASmile.Win.Extend;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmGridColumnImp : FrmBaseModal
    {
        public FrmGridColumnImp(string formName)
        {
            InitializeComponent();
            FormName = formName;
        }

        IGridViewBLL BLLObj { get; }= ObjectFactory.GetObject<IGridViewBLL>("GridViewBLL");

        string FormName { get; }

        //ArgGetList SearchArg { get; } = new Com.GridView.ArgGetList();

        private void FrmGridColumnImp_Load(object sender, EventArgs e)
        {
            dgv.SetDoubleBuffered(true);
            ucPageNav1.InitiOnePageList(new[] { 50, 100, 200 });
            var crud = new FormQuery<ArgGetList, Sys_GridColumn>
            {
                GetListHandle = BLLObj.GetList,
                BtnADSearch = tsAdSearch,                
                BtnSearch = tsBtnSearch,                
                Dgv = dgv,
                Owner = this,
                PageNav = ucPageNav1,
                TxtSearch = tsTxtSearch,
                //OnSearchBefore = SearchBefore
            };
            crud.Init();
        }

        //void SearchBefore(ArgGetList arg)
        //{ 
            
        //}

        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbtnOK_Click(object sender, EventArgs e)
        {
            dgv.EndEdit();
            var selColumns = dgv.GetDataSource<Sys_GridColumn>(s => s.IsSelected);
            int rows = selColumns?.Count() ?? 0;
            if (rows == 0)
            {
                ShowMessage("没有选中任何记录");
                return;
            }
            int success = 0;
            if (!ShowQuestion($"确实要导入数据吗？共记录：{rows} 个", "导入表格列确认")) return;
            this.LoadingShow(AddColumn);
            void AddColumn()
            {
                foreach (var item in selColumns)
                {
                    Sys_GridColumn m = new Sys_GridColumn();
                    Helpers.ReflectionHelper.CopyObject(m, item, "Remark","Id","AddTime");
                    m.FormName = FormName;
                    var r= BLLObj.Add(m);
                    if (!r)
                    {
                        LogHelper.LogObj.Info($"导入列失败，{r.msg}");
                        continue;
                    }
                    success++;
                }
                if (!ShowQuestion($"导入完成，选中：{rows}，成功：{success}，是否退出？", "退出确认")) return;
                this.DialogResult = DialogResult.OK;
            }
        }

        private void tsBtnSelectAll_Click(object sender, EventArgs e)
        {
            var selColumns = dgv.GetDataSource<Sys_GridColumn>(s => true);
            if (selColumns == null) return;
            foreach (var item in selColumns)
            {
                item.IsSelected = true;
            }
            dgv.Refresh();
        }

        private void tsBtnSelectNoAll_Click(object sender, EventArgs e)
        {
            var selColumns = dgv.GetDataSource<Sys_GridColumn>(s => true);
            if (selColumns == null) return;
            foreach (var item in selColumns)
            {
                item.IsSelected = false;
            }
            dgv.Refresh();
        }

        private void tsBtnReverse_Click(object sender, EventArgs e)
        {
            var selColumns = dgv.GetDataSource<Sys_GridColumn>(s => true);
            if (selColumns == null) return;
            foreach (var item in selColumns)
            {
                item.IsSelected = !item.IsSelected;
            }
            dgv.Refresh();
        }
    }
}